package com.jacobson.huobi.mvp.fragment

import android.os.Bundle
import android.preference.*
import com.jacobson.huobi.R
import com.jacobson.huobi.utils.SettingProperties
import com.jacobson.huobi.utils.SoundUtil
import com.trello.rxlifecycle2.components.RxPreferenceFragment

class SettingFragment : RxPreferenceFragment() {
    private lateinit var isMutePreference: SwitchPreference
    private lateinit var ringBuyPreference: PreferenceCategory
    private lateinit var ringSellPreference: PreferenceCategory
    private lateinit var ttsBuyPreference: SwitchPreference
    private lateinit var ttsSellPreference: SwitchPreference
    private lateinit var ringtoneBuyPreference: RingtonePreference
    private lateinit var ringtoneSellPreference: RingtonePreference
    private lateinit var ringCBBuyPreference: MultiSelectListPreference
    private lateinit var ringCBSellPreference: MultiSelectListPreference
    private lateinit var minVolumeValuePreference: EditTextPreference
    private lateinit var ringCBAlarmPreference: ListPreference

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        addPreferencesFromResource(R.xml.preference)
        // 初始化
        initPreferences()
        // 更新状态
        updatePreferencesStatus()
        // 添加事件
        addPreferencesListeners()
    }

    private fun initPreferences() {
        isMutePreference = findPreference(SettingProperties.KEY_IS_MUTE) as SwitchPreference
        ttsBuyPreference = findPreference(SettingProperties.KEY_USE_TTS_BUY) as SwitchPreference
        ttsSellPreference = findPreference(SettingProperties.KEY_USE_TTS_SELL) as SwitchPreference
        ringBuyPreference = findPreference(SettingProperties.KEY_RING_BUY) as PreferenceCategory
        ringSellPreference = findPreference(SettingProperties.KEY_RING_SELL) as PreferenceCategory
        ringtoneBuyPreference =
            findPreference(SettingProperties.KEY_RINGTONE_BUY) as RingtonePreference
        ringtoneSellPreference =
            findPreference(SettingProperties.KEY_RINGTONE_SELL) as RingtonePreference

        ringCBBuyPreference = findPreference(SettingProperties.KEY_TTS_BUY_ITEMS) as MultiSelectListPreference
        ringCBSellPreference = findPreference(SettingProperties.KEY_TTS_SELL_ITEMS) as MultiSelectListPreference
        ringCBAlarmPreference = findPreference(SettingProperties.KEY_TTS_ALARM_ITEMS) as ListPreference

        minVolumeValuePreference = findPreference(SettingProperties.KEY_MIN_VOLUME_VALUE) as EditTextPreference
    }

    private fun updatePreferencesStatus() {
        ringBuyPreference.isEnabled = !isMutePreference.isChecked
        ringSellPreference.isEnabled = !isMutePreference.isChecked
        ringtoneBuyPreference.isEnabled = !ttsBuyPreference.isChecked
        ringtoneSellPreference.isEnabled = !ttsSellPreference.isChecked
        ringtoneBuyPreference.summary = getRingtoneTitle(SettingProperties.getRingtoneBuy())
        ringtoneSellPreference.summary = getRingtoneTitle(SettingProperties.getRingtoneSell())


        ringCBBuyPreference.summary = getCheckBoxSummary(SettingProperties.KEY_TTS_BUY_ITEMS)
        ringCBSellPreference.summary = getCheckBoxSummary(SettingProperties.KEY_TTS_SELL_ITEMS)
//        ringCBAlarmPreference.summary = getSummary(SettingProperties.KEY_TTS_ALARM_ITEMS)

        minVolumeValuePreference.summary = SettingProperties.getMinVolumeValue()
    }

    private fun addPreferencesListeners() {
        // 静音
        isMutePreference.setOnPreferenceChangeListener { _, newValue ->
            val isMute = newValue as Boolean
            ringBuyPreference.isEnabled = !isMute
            ringSellPreference.isEnabled = !isMute
            true
        }
        // TTS
        ttsBuyPreference.setOnPreferenceChangeListener { _, newValue ->
            val useTts = newValue as Boolean
            ringtoneBuyPreference.isEnabled = !useTts
            true
        }
        ttsSellPreference.setOnPreferenceChangeListener { _, newValue ->
            val useTts = newValue as Boolean
            ringtoneSellPreference.isEnabled = !useTts
            true
        }
        ringtoneBuyPreference.setOnPreferenceChangeListener { _, newValue ->
            ringtoneBuyPreference.summary = getRingtoneTitle(newValue as String)
            true
        }
        ringtoneSellPreference.setOnPreferenceChangeListener { _, newValue ->
            ringtoneSellPreference.summary = getRingtoneTitle(newValue as String)
            true
        }

        //设置语音项
        ringCBBuyPreference.setOnPreferenceChangeListener { _, newValue ->
            val hashSet = newValue as HashSet<*>
            var summary = hashSet.toString().replace("[","").replace("]","")
            ringCBBuyPreference.summary = summary
            true
        }
        ringCBSellPreference.setOnPreferenceChangeListener { _, newValue ->
            val hashSet = newValue as HashSet<*>
            var summary = hashSet.toString().replace("[","").replace("]","")
            ringCBSellPreference.summary = summary
            true
        }
        //最低交易量
        minVolumeValuePreference.setOnPreferenceChangeListener{_, newValue ->
            var minVolumeValue = newValue as String
            if (minVolumeValue.isEmpty()) {
                minVolumeValue = "0"
            }
            minVolumeValuePreference.summary = minVolumeValue
            true
        }
        ringCBAlarmPreference.setOnPreferenceChangeListener { _, newValue ->
            var summary  =newValue as String
            ringCBAlarmPreference.summary = summary
            true
        }
    }

    private fun getRingtoneTitle(ringtone: String?): String {
        return SoundUtil.getInstance().getRintoneTitle(activity, ringtone)
    }

    private fun getCheckBoxSummary(key: String?): String {
        var hashSet = PreferenceManager.getDefaultSharedPreferences(activity).getStringSet(key,
            hashSetOf() ) as HashSet<*>
        return  hashSet.toString().replace("[","").replace("]","")

    }

    companion object {
        fun newInstance(): RxPreferenceFragment {
            return SettingFragment()
        }
    }
}